package com.netdisk.demo.controller;

import com.netdisk.demo.entity.Authorities;
import com.netdisk.demo.entity.User;
import com.netdisk.demo.service.AuthoritiesService;
import com.netdisk.demo.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Wang
 * @description 用户管理
 * @date 20:15 2020/5/29
 **/
@RestController
@RequestMapping("/emps")
public class EmpController {
    @Autowired
    private UserService userService;
    @Autowired
    private AuthoritiesService authoritiesService;
    @RequestMapping(value = "/add",method = RequestMethod.POST)
    public Map<String,Object> add(@RequestParam("username")String username,
                                  @RequestParam("password")String password,
                                  @RequestParam("group")String group){
        Map<String,Object> result = new HashMap<>();
        if(userService.queryById(username)==null){
            User user = new User();
            Authorities authorities = new Authorities();
            user.setUserName(username);
            user.setPassword(password);
            authorities.setUserName(username);
            authorities.setAuthority(group);
            userService.insert(user);
            authoritiesService.insert(authorities);
            result.put("msg","添加成功！");
            result.put("result",true);
            return result;
        }
        result.put("msg","添加失败,用户已存在");
        result.put("result",false);
        return result;
    }
    @RequestMapping(value = "/edit",method = RequestMethod.POST)
    public Map<String,Object> edit(){
        Map<String,Object> result = new HashMap<>();//todo:用户编辑部分还没做完
        return result;
    }
    @RequestMapping(value="/del",method = RequestMethod.GET)
    public Map<String,Object> del(@RequestParam("username")String username){
        Map<String,Object> result = new HashMap<>();
        result.put("msg","删除成功！");
        result.put("result",true);
        userService.deleteById(username);
        authoritiesService.deleteById(username);

        return result;
    }
}
